package com.example.zuoye.controller;

import com.example.zuoye.entity.CommentsReply;
import com.example.zuoye.entity.CommentsRoot;
import com.example.zuoye.entity.Liked;
import com.example.zuoye.resultentity.ResultDT;
import com.example.zuoye.service.CommentService;
import com.example.zuoye.utils.ResultDTUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin(origins = "*",maxAge = 3600)   // 跨域
@Slf4j
public class CommentsController {
    @Autowired
    CommentService commentService;

    /**
     * 添加父评论    直接对标文章,资源等下面的评论
     * @param commentsRoot
     * @return
     */
    @PostMapping("addRootComments")
    public ResultDT addRootComments(CommentsRoot commentsRoot){
        log.info("1" + commentsRoot.toString());
        if (commentsRoot.getContent().length() != 0){
            commentsRoot.setCommentId(UUID.randomUUID().toString().replaceAll("-","")); //设置评论唯一标识
            commentsRoot.setCreateTime(new Date()); // 设置添加评论时间
            log.info("2" + commentsRoot);
            boolean comment = commentService.addRootCommentsService(commentsRoot);    // 调用service方法来完成评论的存储
            log.info("3" + commentsRoot.toString());
            if (comment){
                return ResultDTUtils.success(commentsRoot);
            }
        }
        return ResultDTUtils.error(ResultDTUtils.COMMENT_ERROR,"addError");
    }

    /**
     * 根据资源ID来回去该资源的所有评论
     * @param commentsReply
     * @return
     */
    @PostMapping("addSonComments")
    public ResultDT addSonComments(CommentsReply commentsReply) {
        log.info("1" + commentsReply.toString());
        if (commentsReply.getContent().length() != 0) {
            commentsReply.setCommentId(UUID.randomUUID().toString().replace("-", ""));
            commentsReply.setCreateTime(new Date());
            log.info("2"+commentsReply);
            boolean comment = commentService.addSonCommentsService(commentsReply);
            log.info("3"+commentsReply.toString());
            if (comment) {
                return ResultDTUtils.success(commentsReply, null);
            }
        }
        return ResultDTUtils.error(ResultDTUtils.COMMENT_ERROR, "addError");
    }

    /**
     * 根据资源ID来回去该资源的所有评论
     * @param request
     * @return
     */
    @RequestMapping("/getListByOwnerId")
    public ResultDT getListByOwnerId(HttpServletRequest request){
        String ownerId = request.getParameter("ownerId");
        String userId = request.getParameter("userId");
        // 查询所有评论
        List<CommentsRoot> byOwnerIdService = commentService.findByOwnerIdService(ownerId);

        return ResultDTUtils.success(byOwnerIdService);
    }

    /**
     * 点赞模块
     * @param liked
     * @param commType
     * @return
     */
    @PostMapping("isLike")
    public ResultDT isLike(Liked liked,@RequestParam("commType") String commType) {
        boolean comment;
        Liked checked = commentService.checkedLikeService(liked);
        if (checked == null){
            liked.setLikeStatus(1);
            comment = commentService.addLikedService(liked);
        }else{
            if (checked.getLikeStatus() == 0){
                liked.setLikeStatus(1);
            }else{
                liked.setLikeStatus(0);
            }
            comment = commentService.updateLikedService(liked);
        }

        if (comment){
            // 更新评论的点赞次数
            if (liked.getLikeStatus() == 0){
                liked.setLikeStatus(-1);
            }
            if (commType.equals("root")){
                commentService.updateRootLikeNumService(liked);
            }else{
                commentService.updateReplyLikeNumService(liked);
            }
            return ResultDTUtils.success(liked);
        }else{
            return ResultDTUtils.error(ResultDTUtils.SUBMIT_ERROR,"SubmitError");
        }
    }

    @RequestMapping("getListLikeByUserId")
    public ResultDT getListLikeByUserId(@RequestParam("userId") String userId){
        // 查询所有点赞信息
        List<Liked> listLikedService = commentService.getListLikedService(userId);
        return ResultDTUtils.success(listLikedService);
    }
}
